<template>
  <StatisticsHeader ref="StatisticsHeaderRef" />
  <StatisticsSearch class="search_box" :options="statisOptions" @submit="onSubmit"></StatisticsSearch>
  <Quantity ref="QuantityRef" />
  <Statistics ref="StatisticsRef" />
  <Table ref="TableRef" @submit="onTable" />
</template>

<script setup lang="ts">
  import StatisticsHeader from '@/view/smartMarketing/components/StatisticsHeader.vue'
  import StatisticsSearch from '@/components/zw/statistics-search/StatisticsSearch.vue'

  import Quantity from './components/Quantity.vue'
  import Statistics from './components/Statistics.vue'
  import Table from './components/Table.vue'
  import { statisticsUnlimitedPull, infiniteUnlimitedPull, exportUnlimitedPull, lookUnlimitedPull } from '@/api/customer/groupManage/unlimitedPull'
  import { ref, reactive, onMounted, nextTick } from 'vue'
  import { useRoute } from 'vue-router'
  import { downFile } from '@/utils/utils'
  const route = useRoute()
  const StatisticsHeaderRef = ref<any>(null)
  const QuantityRef = ref<any>(null)
  const TableRef = ref<any>(null)
  const StatisticsRef = ref<any>(null)
  const defaultParams = reactive({
    activity_id: route.query.id,
    activity_time: []
  })
  const statisOptions = reactive([
    {
      label: '时间筛选',
      type: 'dateFilter',
    },
  ])
  const onSubmit = (data: any) => {
    defaultParams.activity_time = data.times
    nextTick(() => {
      getAllData()
    })
  }
  const getAllData = (data?: any) => {
    if (data) {
      defaultParams.activity_time = data.activity_time
    }
    let params2 = {
      activity_id: defaultParams.activity_id,
      activity_time: defaultParams.activity_time,
      page: 1,
      page_size: 15
    }
    Promise.all([statisticsUnlimitedPull(defaultParams), infiniteUnlimitedPull(params2)]).then((res: any) => {
      console.log('全部统计数据', res)
      console.log(res[1], '12879381298')

      console.log(res[0]?.summary.in)

      QuantityRef.value.state = {
        total_join: res[0]?.summary.in,
        total_retreat: res[0]?.summary.out,
        total_group: res[0]?.summary.sum
      }
      StatisticsRef.value.state = {
        date: res[0]?.tread.times,
        num_join: res[0]?.tread.in,
        num_retreat: res[0]?.tread.out,
        num_group: res[0]?.tread.sum
      }
      TableRef.value.handleData(res[1])
    })
  }
  const getTableData = (page: any = { page: 1, page_size: 15 }) => {
    infiniteUnlimitedPull({
      ...page,
      ...defaultParams
    }).then((res: any) => {
      TableRef.value.handleData(res)
    })
  }
  const onExport = () => {
    exportUnlimitedPull(defaultParams).then((res: any) => {
      downFile(res.url, res.name)
    })
  }
  const onTable = (type: string, page?: any) => {
    switch (type) {
      case 'export':
        onExport()
        break
      case 'get':
        getTableData(page)
        break
    }
  }
  onMounted(() => {
    lookUnlimitedPull({ id: route.query.id }).then((res: any) => {
      StatisticsHeaderRef.value.state = {
        activity_name: res.title,
        activity_status: res.status_msg,
        period_type: res.period_type,
        begin_date: res.period_start_actual,
        end_date: res.period_end,
        created_at: res.created_at,
        real_end_date: res.period_end_actual,
        created_user: res.create_user_name
      }
    })
  })
</script>
<style lang="scss" scoped></style>
